﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using Fuse8.ContentManagementFramework.SystemFacade.Common;

namespace Fuse8.ContentManagementFramework.Administration.Models.MetaFields
{
    public class MetaValuesProviderManager
    {
        [ImportMany(typeof(IMetaFieldValuesProvider))]
        protected IList<IMetaFieldValuesProvider> AllProviders = new List<IMetaFieldValuesProvider>();

        private static MetaValuesProviderManager facade;

        private MetaValuesProviderManager()
        {
        }

        public static IEnumerable<IMetaFieldValuesProvider> Providers
        {
            get
            {
                if (facade == null)
                {
                    facade = new MetaValuesProviderManager();
                    CmsExtensionManager.LoadExtensions(facade);
                }
                return facade.AllProviders;
            }
        }

        public static IMetaFieldValuesProvider Provider(string name)
        {
            return Providers.FirstOrDefault(p => p.Name == name);
        }
    }
}